package jwm.data;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class URLQueue {

   private List<RatedURL> passed;
   private Queue<RatedURL> queue;
   
   public URLQueue() {
      queue = new LinkedList<RatedURL>();
      passed = new ArrayList<RatedURL>();
   }
   
   public boolean contains(Object object) {
      synchronized (passed) {
         synchronized (queue) {
            return (queue.contains(object) || passed.contains(object));
         }
      }
   }

   public void add(RatedURL ratedURL) {
      synchronized (queue) {
         queue.add(ratedURL);
      } 
   }
   
   public void addPassed(RatedURL ratedURL) {
      synchronized (passed) {
         passed.add(ratedURL);
      } 
   }

   public int left() {
      synchronized (queue) {
         return queue.size();
      }
   }
   
   public int passed() {
      synchronized (passed) {
         return passed.size();
      }
   }

   public RatedURL poll() {
      synchronized (queue) {
         synchronized (passed) {
            RatedURL url = queue.poll();
            passed.add(url);
            return url;
         }
      }
   }
   
   @Override
   public String toString() {
      return "URLQueue [queue=" + queue + "]";
   }
   
}
